package com.ruoyi.web.controller.project.teachpaper;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.project.teachpaper.TeachpaperInfo;
import com.ruoyi.system.domain.project.teachpaper.TeachpaperInfoTeacher;
import com.ruoyi.system.service.project.ITeachpaperInfoService;
import com.ruoyi.system.service.project.ITeachpaperInfoTeacherService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教改论文信息Controller
 * 
 * @author ruoyi
 * @date 2025-10-09
 */
@RestController
@RequestMapping("/project/teachpaper")
public class TeachpaperInfoController extends BaseController
{
    @Autowired
    private ITeachpaperInfoService teachpaperInfoService;
   @Autowired
   private ITeachpaperInfoTeacherService teachpaperInfoTeacherService;
    /**
     * 查询教改论文信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TeachpaperInfo teachpaperInfo)
    {
        startPage();
        List<TeachpaperInfo> list = teachpaperInfoService.selectTeachpaperInfoList(teachpaperInfo);
        return getDataTable(list);
    }

    /**
     * 导出教改论文信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "教改论文信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeachpaperInfo teachpaperInfo)
    {
        List<TeachpaperInfo> list = teachpaperInfoService.selectTeachpaperInfoList(teachpaperInfo);
        ExcelUtil<TeachpaperInfo> util = new ExcelUtil<TeachpaperInfo>(TeachpaperInfo.class);
        util.exportExcel(response, list, "教改论文信息数据");
    }

    /**
     * 获取教改论文信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(teachpaperInfoService.selectTeachpaperInfoById(id));
    }

    /**
     * 新增教改论文信息
     */
    @Log(title = "教改论文信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeachpaperInfo teachpaperInfo)
    {
        TeachpaperInfoTeacher teachpaperInfoTeacher = new TeachpaperInfoTeacher();
        if(teachpaperInfo.getId()!=null){
            teachpaperInfoTeacher.setPaperId(teachpaperInfo.getId());
        }else {
            teachpaperInfoTeacher.setPaperId(-1L);
        }
        List<TeachpaperInfoTeacher> list = teachpaperInfoTeacherService.selectTeachpaperInfoTeacherList(teachpaperInfoTeacher);
        Long currentUserId = teachpaperInfo.getAuthorId() ;
        if(list != null && !list.isEmpty()){
            for (TeachpaperInfoTeacher teacher : list) {
                if(String.valueOf(teacher.getUserId()).equals(String.valueOf(currentUserId))){
                    return error("此论文您已经参与并有名次记录，请勿重复添加");
                }
            }
        }
        return toAjax(teachpaperInfoService.insertTeachpaperInfo(teachpaperInfo));
    }

    /**
     * 修改教改论文信息
     */
    @Log(title = "教改论文信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeachpaperInfo teachpaperInfo)
    {
        return toAjax(teachpaperInfoService.updateTeachpaperInfo(teachpaperInfo));
    }

    /**
     * 删除教改论文信息
     */
    @Log(title = "教改论文信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(teachpaperInfoService.deleteTeachpaperInfoByIds(ids));
    }

    /**
     * 根据关键字查询科研获奖信息
     */
    @GetMapping("/getPaperInfoByTitle")
    public AjaxResult getPaperInfoByTitle(  String data)
    {
        TeachpaperInfo ra = teachpaperInfoService.getPaperInfoByTitle(data);
        return success(ra);
    }
}
